package com.saianfu.flowable.demo.controller

import com.saianfu.flowable.demo.common.impl.HistTaskQueryHandler
import com.saianfu.flowable.demo.common.impl.InstanceHandler
import com.saianfu.flowable.demo.controller.result.ResponseData
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.flowable.task.api.history.HistoricTaskInstance
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 *
 *
 * 流程实例相关
 *
 *
 */
@RestController
@RequestMapping("api/flow/instance")
@Api(value = "Instance", tags = ["流程实例"])
class InstanceController {
    @Autowired
    lateinit var instanceHandler: InstanceHandler

    @Autowired
    lateinit var histTaskQueryHandler: HistTaskQueryHandler

    /**
     * 启动流程实例
     */
    @RequestMapping(value = ["/startByKey"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "启动流程实例__通过流程定义name", notes = "实例启动成功，返回当前活动任务", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "name", value = "流程定义name", required = true, dataType = "String"))
    fun startByName(name: String, @RequestBody variables: Map<String, Any?>): ResponseData<HistoricTaskInstance?> {
        val pi = instanceHandler.startProcessInstanceByKey(name, variables)
        val historicTaskInstance = histTaskQueryHandler.activeTask(pi.processInstanceId)
        return ResponseData.success(historicTaskInstance)
    }

    /**
     * 启动流程实例
     */
    @RequestMapping(value = ["/startById"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "启动流程实例--通过流程定义ID", notes = "实例启动成功，返回当前活动任务", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "id", value = "流程定义ID", required = true, dataType = "String"))
    fun startById(id: String, @RequestBody variables: Map<String, Any?>): ResponseData<HistoricTaskInstance?> {
        val pi = instanceHandler.startProcessInstanceById(id, variables)
        val historicTaskInstance = histTaskQueryHandler.activeTask(pi.processInstanceId)
        return ResponseData.success(historicTaskInstance)
    }

    /**
     * 启动流程实例
     */
    @RequestMapping(value = ["/startAndExecute"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "启动流程实例并执行第一个流程任务", notes = "返回已执行、活动的任务map", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "name", value = "流程定义KEY（模板ID）", required = true, dataType = "String"), ApiImplicitParam(name = "userId", value = "流程启动者ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun startAndExecute(name: String, userId: String, @RequestBody variables: Map<String, Any?>): ResponseData<*> {
        val map: Map<String, Any?> = instanceHandler.startInstanceAndExecuteFirstTask(name, userId, variables)
        return ResponseData.success(map)
    }

    /**
     * 启动流程实例
     */
    @RequestMapping(value = ["/startExecuteAndSetActor"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "启动流程实例并执行第一个流程任务,并设置下一任务处理人", notes = "{\"days\":\"6\",\"actorIds\":[\"zhangsan\",\"lisi\"]}", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "name", value = "流程定义KEY（模板ID）", required = true, dataType = "String"), ApiImplicitParam(name = "userId", value = "流程启动者ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun startAndExecuteAndSetActor(name: String, userId: String, @RequestBody variables: Map<String, Any?>): ResponseData<*> {
        val map: Map<String, Any?> = instanceHandler.startInstanceAndExecuteFirstTask(name, userId, variables)
        return ResponseData.success(map)
    }

    /**
     * 挂起流程实例
     */
    @RequestMapping(value = ["/suspendById"], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "挂起流程实例", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "processInstanceId", value = "流程实例ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun suspendById(processInstanceId: String): ResponseData<*> {
        instanceHandler.suspendProcessInstanceById(processInstanceId)
        return ResponseData.success("流程实例挂起成功")
    }

    /**
     * 激活流程实例
     */
    @RequestMapping(value = ["/activateById"], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "激活流程实例", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "processInstanceId", value = "流程实例ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun activateById(processInstanceId: String): ResponseData<*> {
        instanceHandler.activateProcessInstanceById(processInstanceId)
        return ResponseData.success("激活流程实例")
    }

    companion object {
        protected var logger = LoggerFactory.getLogger(InstanceHandler::class.java)
    }
}